﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataContexts;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            UserAccess.ClearUserSession();
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            UserDataContext context = new UserDataContext();
            User user = context.Users.SingleOrDefault(e1 => e1.Username == txtUsername.Text);
            if (user != null && string.Compare(user.Password, txtPassword.Text, false) == 0)
            {
                UserAccess.CurrentUser = user;
                if (UserAccess.IsCurrentUser(Enums.UserTypes.Supplier))
                {
                    UserAccess.CurrentSupplier = (new DataContexts.Supplier.SupplierDataContext()).Suppliers.SingleOrDefault(s => s.UserId == user.UserId); ;
                }

                Response.Redirect(UserAccess.CurrentUserType.HomePageUrl);
            }
            else
            {
                divErrorMessage.InnerHtml = "Invalid username or password<br/><br/>";
            }
        }
    }
}
